const path = require('path')
const Koa = require('koa')
const parameter = require('koa-parameter')
const KoaStatic = require('koa-static')
const router = require('../router/index')
const errHandler = require('./errorHandler')
const { koaBody } = require('koa-body')

const app = new Koa()

app.use(koaBody({
    multipart: true,
    formidable: {
        uploadDir: path.join(__dirname, '../upload'),
        keepExtensions: true
    },
    parsedMethods: ['POST', 'PATCH', 'PUT', 'DELETE']
}))
app.use(KoaStatic(path.join(__dirname, '../upload')))
app.use(parameter(app))
app.use(router.routes()).use(router.allowedMethods())

app.on('error', errHandler)

module.exports = app
